package cn.b.training.baby_education_training.service.impl;

import cn.b.training.baby_education_training.config.BaiduAiProperties;
import com.baidu.aip.face.AipFace;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bos.BosClient;
import com.baidubce.services.bos.BosClientConfiguration;
import com.baidubce.services.bos.model.PutObjectResponse;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.HashMap;

@Component
public class BaiduAiService {
    @Autowired
    BaiduAiProperties baiduAiProperties;
    /**
     * 人脸检测功能
     * @param imageBase64 图片base64字符串
     * @return 解析结果
     */
    public JSONObject checkFaces(String imageBase64){
        //实例化百度ai的SDK
        AipFace client = new AipFace(
                baiduAiProperties.getAppId(),
                baiduAiProperties.getApiKey(),
                baiduAiProperties.getSecretKey());
        // 传入可选参数调用接口
        HashMap options = new HashMap();
        options.put("face_field", "age,beauty,expression,face_shape,gender,glasses,landmark,landmark72,landmark150,quality,eye_status,emotion,face_type");
        options.put("max_face_num","10");
        options.put("face_type","LIVE");
        options.put("liveness_control", "NONE");

        String imageType = "BASE64";

        // 人脸检测
        JSONObject res = client.detect(imageBase64, imageType, options);
        return res;
    }

    public JSONObject checkFacesByUrl(String imageUrl) {
        //实例化百度ai的SDK
        AipFace client = new AipFace(
                baiduAiProperties.getAppId(),
                baiduAiProperties.getApiKey(),
                baiduAiProperties.getSecretKey());
        // 传入可选参数调用接口
        HashMap options = new HashMap();
        options.put("face_field", "age,beauty,expression,face_shape,gender,glasses,landmark,landmark72,landmark150,quality,eye_status,emotion,face_type");
        options.put("max_face_num","10");
        options.put("face_type","LIVE");
        options.put("liveness_control", "NONE");

        String imageType = "URL";

        // 人脸检测
        JSONObject res = client.detect(imageUrl, imageType, options);
        return res;
    }

    public String putFile(String fileKey, InputStream inputStream){

        // 初始化一个BosClient
        BosClientConfiguration config = new BosClientConfiguration();
        config.setCredentials(new DefaultBceCredentials(
                baiduAiProperties.getAccessKeyId(),
                baiduAiProperties.getSecretAccessKey()));
        config.setEndpoint("hzw-bos.bj.bcebos.com");
        BosClient client = new BosClient(config);
        PutObjectResponse putObjectResponse = client.putObject("baby",fileKey,inputStream);
        String etag = putObjectResponse.getETag();
        return etag;
    }
    public String putFile1(String fileKey, InputStream inputStream){

        // 初始化一个BosClient
        BosClientConfiguration config = new BosClientConfiguration();
        config.setCredentials(new DefaultBceCredentials(
                baiduAiProperties.getAccessKeyId(),
                baiduAiProperties.getSecretAccessKey()));
        config.setEndpoint("hzw-bos.bj.bcebos.com");
        BosClient client = new BosClient(config);
        PutObjectResponse putObjectResponse = client.putObject("encyclopedia",fileKey,inputStream);
        String etag = putObjectResponse.getETag();
        return etag;
    }
    public String putFile2(String fileKey, InputStream inputStream){

        // 初始化一个BosClient
        BosClientConfiguration config = new BosClientConfiguration();
        config.setCredentials(new DefaultBceCredentials(
                baiduAiProperties.getAccessKeyId(),
                baiduAiProperties.getSecretAccessKey()));
        config.setEndpoint("hzw-bos.bj.bcebos.com");
        BosClient client = new BosClient(config);
        PutObjectResponse putObjectResponse = client.putObject("food-menu",fileKey,inputStream);
        String etag = putObjectResponse.getETag();
        return etag;
    }
    public String putFile3(String fileKey, InputStream inputStream){

        // 初始化一个BosClient
        BosClientConfiguration config = new BosClientConfiguration();
        config.setCredentials(new DefaultBceCredentials(
                baiduAiProperties.getAccessKeyId(),
                baiduAiProperties.getSecretAccessKey()));
        config.setEndpoint("hzw-bos.bj.bcebos.com");
        BosClient client = new BosClient(config);
        PutObjectResponse putObjectResponse = client.putObject("note",fileKey,inputStream);
        String etag = putObjectResponse.getETag();
        return etag;
    }
}
